package com.atguigu03.threadsafe.exer;

/**
 * @author NoobMaster69  * @date 2024/6/12 0012 星期三 20:27
 */


class Account{
    private double balance;

    public synchronized void deposit(double amt){
        balance += amt;
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "用户，存入1000：当前余额" + balance);
    }
}
class Customer implements Runnable{

    Account account;

    public Customer(Account acct){
        this.account = acct;
    }

    @Override
    public void run() {
        for (int i = 0; i < 3; i++) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            account.deposit(1000);
        }
    }
}
public class Test {
    public static void main(String[] args) {
        Account acct = new Account();

        Customer c1 = new Customer(acct);
        Customer c2 = new Customer(acct);

        Thread t1 = new Thread(c1);
        Thread t2 = new Thread(c2);

        t1.setName("线程1");
        t2.setName("线程2");

        t1.start();
        t2.start();


    }
}
